const Koa = require("koa")
const app = new Koa()
const json = require("koa-json")  //将http相应的数据转化为json格式
const bodyParser = require('koa-bodyparser')  //解析http请求的消息体
const cor = require('@koa/cors')
const {addAliases} = require('module-alias')
const koaRedis = require('ioredis')
addAliases({
    "@":__dirname
})

const router = require('@/router')
// 统一接口响应数据格式
const responseHandler =require('@/config/result')
const errorHandler = require('@/config/errorhandler')

app.use(json())
app.use(bodyParser())
app.use(cor())
app.use(responseHandler)
app.use(errorHandler)
// 连接reids
const redisClient = new koaRedis({
    port:6379,
    host:"127.0.0.1"
})
// 将resid注册进中间件
app.use(async (ctx,next)=>{
    ctx.redis=redisClient
    await next()
})
app.use(router.routes()).use(router.allowedMethods());


app.listen(7000);
console.log('7000端口已启动!')